templateViewInit = ()=>{
    var emailList = [
        {smtp:'smtp.qq.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.163.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.126.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.sina.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.exmail.qq.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.sohu.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.live.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.gmail.com',port:'25',ssl:'465',tls:'587'},
        {smtp:'smtp.mail.yahoo.com',port:'25',ssl:'465',tls:'587'},
    ]
    searchFormInfo();
    // 已知数据渲染
    var inst = table.render({
        elem: '#email-account-list',
        id:'email-account-list-id',
        height: '#content-body-150',
        url: top.tools.getBaseUrl()+'/email/account/getPage',
        method: 'post',
        headers:{
            'Access-Token':top.tools.cookie.token(),
            'Content-Type':'application/json',
        },
        toolbar: '#emailToolbar',
        defaultToolbar: ['filter', 'exports', 'print', { // 右上角工具图标
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips',
            onClick: function (obj) { // 2.9.12+
                layer.alert('自定义工具栏图标按钮');
            }
        }],
        cols: [
            [
                {type: 'checkbox', width: 50},
                {type: 'numbers', title: '序号', align: 'center', width: 80},
                {field: 'mail', title: '邮箱', align: 'left', width: 200},
                {field: 'username', title: '用户名', align: 'left',width: 200},
                {field: 'password', title: '密码', align: 'left',width: 300,templet:function (d){
                    return d.password.replace(/./g, "*");
                    }},
                {field: 'host', title: '服务器域名', align: 'left',minWidth: 120},
                {field: 'port', title: '服务器端口', align: 'center',minWidth: 120},
                {field: 'createTime', title: '创建时间', align: 'center',width: 160,templet:function (d){
                        return top.tools.form.formatDate(d.createTime);
                }},
                {fixed: 'right', title: '操作', align: 'center', width: 180, minWidth: 125, toolbar: '#barDemo'}
            ]
        ],
        parseData: dataToFormat,
        data:[
            {id:1,name:'用户性别',type:'system_user_sex',status:'0',remark:'',createTime:'2021-01-05 17:03:48'}
        ],
        //skin: 'line', // 表格风格
        //even: true,
        page: true, // 是否显示分页
        limits: [15, 20, 30,50],
        limit: 15, // 每页默认显示的数量
        done: function (res, curr, count) {
            $(".layui-table th[data-field=" + 0 + "] .layui-table-cell").html("");
        }
    });

    // 工具栏事件
    table.on('toolbar(email-account-list)', function (obj) {
        var id = obj.config.id;
        var checkRow = table.checkStatus(id);

        var data = checkRow.data;
        if (obj.event === 'add') {
            openForm({
                title:"新增邮箱账号",
                btn:1,
                save:(closeid)=>{
                    formFun.save(inst,closeid)
                },
                reset:(closeid)=>{
                    form.val('email-account-form', top.tools.form.getFindElementOnObj("#lay-email-account-form",false));
                },
                before:(index,layero,that,isEdit)=>{
                    formFun.init(layero,emailList,isEdit)
                },
                area:'40%'
            },'email-account-form')
        } else if (obj.event === 'update') {
            if (checkRow.data.length<=0){
                layer.msg("请选择数据！");
                return
            }
            openForm({
                title:"查看邮箱账号",
                btn:-1,
                area:'40%',
                before:(index,layero,that,isEdit)=>{
                    form.val('email-account-form', data);
                }
            },'email-account-form')
        } else  if (obj.event === 'del') {
            if (checkRow.data.length<=0){
                layer.msg("请选择数据！");
                return
            }
            let ids = data.map((user) => user.id);
            openConfirm(()=>{
                formFun.remove({
                    id:ids.join(',')
                },inst)
            });
        }
    });
// 触发单元格工具事件
    table.on('tool(email-account-list)', function (obj) { // 双击 toolDouble
        var data = obj.data; // 获得当前行数据
        if (obj.event === 'edit') {
            openForm({
                title:"编辑邮箱账号",
                btn:1,
                save:(closeid)=>{
                    formFun.save(inst,closeid)
                },
                reset:(closeid)=>{
                    form.val('email-account-form', top.tools.form.getFindElementOnObj("#lay-email-account-form",false));
                },
                area:'40%',
                before:(index,layero,that,isEdit)=>{
                    formFun.init(layero,emailList,isEdit)
                    form.val('email-account-form', data);
                }
            },'email-account-form')
        } else if (obj.event === 'view') {
            openForm({
                title:"查看邮箱账号",
                btn:-1,
                area:'40%',
                before:(index,layero,that,isEdit)=>{
                    formFun.init(layero,emailList,isEdit)
                    form.val('email-account-form', data);
                }
            },'email-account-form')
        } else if (obj.event === 'del') {
            openConfirm(()=>{
                formFun.remove(data,inst)
            });
        }
    });

    queryButton('email-account-list-id');
    formReset();
}

let formFun = {
    init:(layero,emailList,isEdit)=>{
        workdbFieldConvert(document.querySelectorAll('#lay-email-account-form .dict-module'),isEdit);
        form.render($(layero).find('.layui-form'));
        for (let i = 0; i < emailList.length; i++) {
            $('select[name=host]').append("<option value='"+emailList[i].smtp+"'>"+emailList[i].smtp+"</option>")
        }
        // 定向渲染 select
        form.render(layero.find('.layui-form select'));
        var selectElem = layero.find('.layui-form-select');
        layero.find('.layui-layer-content').on('scroll', function() {
            selectElem.removeClass('layui-form-selected');
            layui.$('.layui-select-panel-wrap').detach();
        });
        form.on('switch(ssl)', (data)=>{
            var elem = data.elem;
            var checked = elem.checked;
            if (checked){
                $('input[name=starttlsEnable]').prop('checked',false);
                $('input[value=465]').prop('checked',true);
            }else {
                if (!$('input[name=starttlsEnable]').prop('checked')){
                    $('input[value=25]').prop('checked',true);
                }
            }
        });
        form.on('switch(tls)', (data)=>{
            var elem = data.elem;
            var checked = elem.checked;
            if (checked){
                $('input[name=sslEnable]').prop('checked',false);
                $('input[value=587]').prop('checked',true);
            }else {
                if (!$('input[name=sslEnable]').prop('checked')){
                    $('input[value=25]').prop('checked',true);
                }
            }
        });
        $('select[name=host]').next().find('input').removeAttr("readonly");
        $('input[name=mail]').on('input', function () {
            let value = $(this).val()
            $('input[name=username]').val(value)
        });
    },
    save:(emailTable,closeid)=>{
        let formData = top.tools.form.getFindElementOnObj("#lay-email-account-form",true);
        if (typeof formData === 'boolean'){
            return;
        }

        top.API.email().mailAccountSave(formData,(res)=>{
            layer.msg(res.message, {icon: 1}, function(){
                // layer.msg('提示框关闭后的回调');
                if (emailTable){
                    table.reloadData('email-account-list-id', {
                        scrollPos: true, // 设定重载数据或切换分页时的滚动条的位置状态 --- 属性设置有效
                        page: {
                            curr: emailTable.config.page.curr,
                            limit: emailTable.config.page.limit
                        }
                    });
                }
                if (closeid){
                    layer.close(closeid);
                }
            });
        })
    },
    remove:(data,emailTable)=>{
        top.API.email().mailAccountDel(data,(res)=>{
            layer.msg(res.message, {icon: 1}, function(){
                // layer.msg('提示框关闭后的回调');
                if (emailTable){
                    table.reloadData('email-account-list-id', {
                        scrollPos: true, // 设定重载数据或切换分页时的滚动条的位置状态 --- 属性设置有效
                        page: {
                            curr: emailTable.config.page.curr,
                            limit: emailTable.config.page.limit
                        }
                    });
                }
            });
        })
    },
}